<?php


namespace app\common\traites;


use app\common\model\AgentLavel;
use app\common\model\Luzhu;

trait TraitModel
{
    /**
     * 直接删除
     * @param $id /主键
     * @return bool
     */
    public function del($id)
    {
        $find = $this->find($id);
        if (empty($find)) return false;
        return $find->delete();
    }

    /**
     * 添加数据
     * @param $data /数据数组
     * @param bool $type /true 单条添加,false多条添加
     */
    public function add(array $data, bool $type = true)
    {
        //单条添加
        if ($type) {
            return $this->insert($data);
        }
        //多条添加
        return $this->insertAll($data);
    }

    //软删除
    public function deletes($id)
    {
        return '';
    }

    public function setStatus($post)
    {
        $id = intval($post['id']);
        $status = intval($post['status']);
        //$status = $post['status'] == 1 ? 0 : 1;

        if ($id < 1) return false;
        $find = $this->find($id);
        return $find->save(['status' => $status]);
    }

    //图片转换地址
    public function getThumbUrlAttr($value)
    {
        if (empty($value)) return '';
        if (is_array($value)) return '';
        $value = explode(',', $value);
        if (count($value) > 1) {
            foreach ($value as $key => $v) {
                $value[$key] = config('ToConfig.app_update.image_url') . $v;
            }
            return $value;
        }
        return config('ToConfig.app_update.image_url') . $value[0];
    }

    //荷官转换地址
    public function getHeGuanHeadImgAttr($value)
    {
        if (empty($value)) return '';
        if (is_array($value)) return '';
        $value = explode(',', $value);
        if (count($value) > 1) {
            foreach ($value as $key => $v) {
                $value[$key] = config('ToConfig.app_update.image_url') . $v;
            }
            return $value;
        }
        return config('ToConfig.app_update.image_url') . $value[0];
    }

    //视频转换地址
    public function getVideoUrlAttr($value)
    {
        if (!empty($value)) return config('ToConfig.app_update.image_url') . $value;
        return '';
    }


    //代理商查看代理商推广的用户充值等 不排除自己
    public static function whereMap($alias = 'a.id')
    {
        $map = [];
        //代理商 推广列表
//        if (session('admin_user.agent')){
//            $map = ['agent_id_1|agent_id_2|agent_id_3|b.id' => session('admin_user.id')];
//        }
        if (session('admin_user.agent')){
            $user = AgentLavel::agent_down(session('admin_user.id'));
            $user = implode(',', $user);
            $map[] = [$alias, 'in', $user];
        }
        return $map;
    }

    //代理商查看用户代理。排除自己
    public static function whereMapUser($true = null)
    {
        $map = [];
        //代理商 推广列表
         if (session('admin_user.agent')) {
            //查询用户所有的下级
            $user = AgentLavel::agent_down(session('admin_user.id'));

            // $map = ['agent_id_1|agent_id_2|agent_id_3' => session('admin_user.id')];
            $map[] = ['a.agent_id', 'in', implode(',', $user)];

            $id_string = [];
            foreach ($user as $key=>&$value){
                if ($value == session('admin_user.id')){
                    continue;
                }
                $id_string [$key]= $value;
            }
            $map[] = ['a.id', 'in', implode(',', $id_string)];

        }  elseif ($true) {
            $user = AgentLavel::agent_down($true->id);
            //不是代理商是查询某各代理商下面所有人信息 $true 代理商个人信息
            //$map = ['agent_id_1|agent_id_2|agent_id_3|a.id' => $true->id];
            $user = implode(',', $user);
            $map[] = ['a.id', 'in', $user];
        }


        return $map;
    }

    //时间表达式查询
    public static function get_where_time_by($model, $start, $end, $type, $field = 'created_at')
    {
        //时间表达式判断
        if (!empty($start) && !empty($end)) {
            $model = $model->whereTime($field, 'between', [$start, $end]);
        } elseif (!empty($start)) {
            $model = $model->whereTime($field, '>=', $start);
        } elseif (!empty($end)) {
            $model = $model->whereTime($field, '<=', $end);
        } elseif ($type > 0) {
            switch ($type) {
                case 1:
                    $model = $model->whereTime($field, 'today');
                    break;
                case 2:
                    $model = $model->whereTime($field, 'yesterday');
                    break;
                case 3:
                    $model = $model->whereTime($field, 'week');
                    break;
                case 4:
                    $model = $model->whereTime($field, 'month');
                    break;
            }
        }
        return $model;
    }

    //输赢样式输出
    public static function win_or_lose_calculation($result): array
    {
        if (!$result) return [];
        $array = explode('|', $result);
        if (!is_array($array)) return [];
        $main = [1 => '庄', 2 => '闲', 3 => '和', 4 => '幸',];
        $vice = [0 => '', 1 => '庄对', 2 => '闲对', 3 => '庄闲对'];
        $zhuang = isset($array[0]) && isset($main[$array[0]]) ? $main[$array[0]] :'';
        $idle = isset($array[1]) && isset($vice[$array[1]])? $vice[$array[1]] :'';
        return ['main' =>$zhuang, 'vice' => $idle];
    }

    public static function lz_win_or_lose_calculation($item): array
    {
        if (!$item) return [];
        if ($item->lu_zhu_id <= 0) return [];
        //查询露珠
        $result = Luzhu::where('id',$item->lu_zhu_id)->value('result');
        return self::win_or_lose_calculation($result);
    }
}